package com.geofitti.andoird;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.*;
import org.apache.http.ParseException;
import org.apache.http.client.*;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.net.*;
import android.net.http.*;

public class GeoFittiActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.geofitti_login);
        
        KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
        KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
        lock.disableKeyguard();
       
        final EditText usernameText = (EditText) findViewById(R.id.log_user);
		final EditText passwordText = (EditText) findViewById(R.id.log_pass);
        final Button loginBtn = (Button) findViewById(R.id.btnLogin);
        TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
      
        loginBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	try {
            		String paramString = "?username=" + usernameText.getText() + "&password=" + passwordText.getText();
					JSONObject jsonO = WebCall.doGet("http://icarus.cs.weber.edu/~km06559/geofitti.php/login" + paramString);
					if (!jsonO.getString("status").equals("ok")){ //login failed
						AlertDialog.Builder builder = new AlertDialog.Builder(GeoFittiActivity.this);
						builder.setMessage(jsonO.getString("message"))
						       .setCancelable(false)
						       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
						           public void onClick(DialogInterface dialog, int id) {
										   dialog.dismiss();
						           }
						       });
						AlertDialog alert = builder.create();
						alert.show();
					}else{
						Intent i = new Intent(getApplicationContext(), MenuActivity.class);
				       	startActivity(i);
				        GeoFittiActivity.this.finish(); 
					}
					
					
            	} catch (JSONException e) {
					// TODO Auto-generated catch block 
					e.printStackTrace();
				}
            }
        });
        
        registerScreen.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(i);
             
   /*         	
            	JSONObject json = new JSONObject();
            	try {
					json.put("sent", "todizzle3");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            	String url = "http://icarus.cs.weber.edu/~km06559/index.php";
            	HttpResponse re;
            	String temp = new String();
            	try {
					re = doPost(url, json);
					temp = EntityUtils.toString(re.getEntity());
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	*/				
            	
            }
        });
        
    }
    
    public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException
	{
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost request = new HttpPost(url);
	    HttpEntity entity;
	    StringEntity s = new StringEntity(c.toString());

	    s.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
	    entity = s;
	    request.setEntity(entity);
	    HttpResponse response;
	    response = httpclient.execute(request);
	    return response;
	}
    
}
